package com.startup.yellocar.component;

import java.util.List;

import javax.security.auth.kerberos.ServicePermission;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.startup.yellocar.dao.SMSProviderDAO;
import com.startup.yellocar.dto.SMSProviderDTO;
import com.startup.yellocar.utils.Constant;



@Component
public class SMSServiceProviderFactory {

	@Autowired
	private ApplicationContext applicationContext;
	
	@Autowired
	SMSProviderDAO smsProvider;
	
	@Autowired
	ProblemManager problemManager;
	
	public SMSProviderService getSMSProviderService(){
		List<SMSProviderDTO> listOfSMSProvider = smsProvider.getByCredit(1);
		if(listOfSMSProvider==null || listOfSMSProvider.size()<=0)
		{
			problemManager.alert(Constant.ALERT_FATAL ,"Any SMS provider found",null);
			return null;
		}
		SMSProviderDTO smsProviderDTO = listOfSMSProvider.get(0);
		SMSProviderService smsService = (SMSProviderService)applicationContext.getBean(smsProviderDTO.getServiceName());
		if(smsService==null)
		{
			problemManager.alert(Constant.ALERT_FATAL ,"SMS Service not found for provider "+smsProviderDTO.getName(),null);
			return null;
		}
		smsService.setAccessToken(smsProviderDTO.getToken());
		smsService.setUrl(smsProviderDTO.getUrl());
		return smsService;
	}
	
	
}
